package com.YxyBackMange.StatcManger;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/4/23.
 */
public class StaticHandlerInterceptor implements HandlerInterceptor {
    private String configLocation;
    private boolean isDebug = false;
    private boolean isStatic = false;
    private String path = "sta";
    public String getConfigLocation() {
        return configLocation;
    }

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
        this.init();
    }

    //请求
    @Override
    public boolean preHandle(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object handler) throws Exception {
        System.out.println("-------------请求开始:" + System.currentTimeMillis());
        if (!StaticManger.isIsStatic()) {
            System.out.println("没有开启拦截--执行放行操作");
            return true;
        }
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String url = request.getRequestURI();
        String queryurl = request.getQueryString();
        boolean flag = false;
        for (StaticConfig sc : StaticManger.getConfigs()) {
            List<String> values = new ArrayList<String>();
            if (sc.getGenerateUrl().getUrl().equals(url)) {
                if (StaticManger.isIsDebug()) {
                    System.out.println("静态化地址匹配成功:" + request.getRequestURI());
                }
                for (String p : sc.getGenerateUrl().getPars()) {
                    String value = request.getParameter(p);
                        values.add(value);
                }
                sc.setValues(values);
                return StaticManger.forward(sc, request, response);
            }
            for (UrlPar urlPar : sc.getRefreshUrls()) {
                if (urlPar.getUrl().equals(url)) {
                    if (StaticManger.isIsDebug()) {
                        System.out.println("更新匹配成功:" + request.getRequestURI());
                    }
                    for (String p : urlPar.getPars()) {
                        String value = request.getParameter(p);
                        values.add(value);
                    }
                    sc.setValues(values);
                    return StaticManger.clear(sc, request, response);
                }
            }

        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("-------------请求结束:" + System.currentTimeMillis());
    }

    public void init() {
        String path = configLocation;
        String basPath = null;
        try {
            basPath = StaticHandlerInterceptor.class.getClassLoader().getResource("").toURI().getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        if (path == null || path == "") {
            path = "WEB-INF" + File.separatorChar + "static-config.xml";
        }
        XMLUtil xmlUtil = new XMLUtil(new File(basPath + File.separatorChar + path));
        StaticManger.setConfigs(xmlUtil.getConfig());
        if (StaticManger.isIsDebug()) {
            System.out.println("静态化插件初始化成功!");
            System.out.println(StaticManger.getConfigs());
        }
    }

    public boolean isDebug() {
        return isDebug;
    }

    public void setDebug(boolean debug) {
        isDebug = debug;
        StaticManger.setIsDebug(debug);
    }

    public boolean isStatic() {
        return isStatic;
    }

    public void setStatic(boolean aStatic) {
        System.out.println("拦截情况:" + aStatic);
        StaticManger.setIsStatic(aStatic);
        this.isStatic = aStatic;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
        StaticManger.setPath(path);
    }
}
